﻿
Partial Class krajaymonths
    Inherits System.Web.UI.Page
    Public ScriptText, CloseText As String
    Dim lockhash As New Hashtable

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Response.Write(Request("BDID"))
        Dim adapter As New SplitData2TableAdapters.Budget12TableAdapter
        Dim datatable As New SplitData2.Budget12DataTable
        Dim datatable2 As New SplitData2.Budget12DataTable
        Dim info_array() As String = Split(Request("BDID"), ";")
        If IsPostBack = False Then
            For i = 0 To info_array.Length - 1
                If info_array(i) <> String.Empty Then

                    datatable = adapter.GetDataByGroupID(info_array(i))
                    datatable2.ImportRow(datatable.Rows(0))
                End If
            Next
            GridView1.DataSource = datatable2
            GridView1.DataBind()
        End If

        Dim prefil2 As String = ""
        prefil2 = Session("prefilter")
        Dim re_adap As New FormLockDSTableAdapters.FundCenterReleasesTableAdapter
        Dim rsql As String = ""
        rsql = " SELECT FundCenterReleases.FundCenter_FundCenterCode FROM FundCenterReleases "
        rsql += " WHERE PlanYear = " & Val(Session("PlanYear")) & " "
        rsql += " AND Version = '" & Session("Versions").ToString & "' "
        rsql += " AND FundCenter_FundCenterCode IN (" & prefil2 & ")" : rsql += " AND ReleaseFlag = 'Y' "


        Dim re_dt As New FormLockDS.FundCenterReleasesDataTable
        re_adap.GetDataByFilter(re_dt, rsql)
        For Each ritem As FormLockDS.FundCenterReleasesRow In re_dt
            lockhash.Remove(ritem.FundCenter_FundCenterCode) : lockhash.Add(ritem.FundCenter_FundCenterCode, True)
        Next

    End Sub
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim RunningNo As Label = e.Row.Cells(0).FindControl("RunningNo")


        If Not (RunningNo Is Nothing) Then
            RunningNo.Text = e.Row.RowIndex + 1
        End If
    End Sub
    Function GenerateFundCName(ByVal ccode As String) As String
        Dim adapter As New SplitBudgetDSTableAdapters.FundCentersTableAdapter
        Return adapter.GetNames(ccode)
    End Function
    Function GenerateCommitName(ByVal ccode As String) As String

        Dim adapter As New SplitBudgetDSTableAdapters.CmmtsTableAdapter

        Return adapter.GetNames(ccode)
    End Function
    Function GetAmount(ByVal fundccode As String, ByVal cmmtcode As String, ByVal CmmtDetail As String, ByVal Prodcode As String, ByVal projCode As String, ByVal ActiCode As String, ByVal Actype As String, ByVal version As String, ByVal years As String) As Decimal

        Dim amount As Decimal = 0
        Dim adapter1 As New SplitData2TableAdapters.BudgetDetails1TableAdapter
        amount = adapter1.GetSumAmount(fundccode, cmmtcode, CmmtDetail, Actype, ActiCode, projCode, Prodcode, years, version)

        Return amount
    End Function
    Protected Sub ClickedButton_Click(sender As Object, e As EventArgs) Handles ClickedButton.Click
        Dim adapterx As New SplitData2TableAdapters.BudgetDetails1TableAdapter
        Dim addedstring As String = ""
        For i = 0 To CheckBoxList1.Items.Count - 1

            If CheckBoxList1.Items(i).Selected Then

                addedstring += i + 1 & ";"

            End If

        Next
        Dim info_array() As String = Split(addedstring, ";")

        For Each row As GridViewRow In GridView1.Rows
            Dim fctr_code As Label = CType(row.FindControl("fundccode"), Label)
            If lockhash(fctr_code.Text.ToString.Trim) = True Then
                Continue For
            End If

            Dim countsql = ""

            Dim b_id As Label = CType(row.FindControl("bid"), Label)

            Dim s_id As Label = CType(row.FindControl("IDX"), Label)



            'Dim fundccode As Label = CType(row.FindControl("fundcCode"), Label)
            'Dim cmmtcode As Label = CType(row.FindControl("CmmtCode"), Label)
            'Dim cmmtdetail As Label = CType(row.FindControl("CmmtDetail"), Label)
            'Dim projcode As Label = CType(row.FindControl("projCode"), Label)
            'Dim acticode As Label = CType(row.FindControl("actiCode"), Label)
            'Dim actypecode As Label = CType(row.FindControl("Actitypecode"), Label)
            'Dim prodcode As Label = CType(row.FindControl("Prodcode"), Label)
            'Dim vers As Label = CType(row.FindControl("version"), Label)
            'Dim pyear As Label = CType(row.FindControl("planyear"), Label)
            'Dim fundedprogram As Label = CType(row.FindControl("fp"), Label)
            'Dim FundCenter_FundCenterCode, Product_ProductCode, Cmmt_CmmtCode, Project_ProjectCode, ActivityType_ActivityTypeCode, Activity_ActivityCode As String
         
            countsql += "        SELECT        Amount As SumAmount"
            countsql += " FROM BudgetDetails"
            countsql += " WHERE Id = " & Val(b_id.Text)
            'countsql += " WHERE 1=1   AND (PlanYear = '" & pyear.Text & "') AND (Version = '" & vers.Text & "') "
            'FundCenter_FundCenterCode = fundccode.Text

            '    If cmmtcode.Text <> "" Then
            '        countsql += " And Cmmt_CmmtCode = '" & cmmtcode.Text & "' "
            '    Else
            '        countsql += " And Cmmt_CmmtCode is null "
            '    End If




            '    If actypecode.Text <> "" Then
            '        countsql += " And ActivityType_ActivityTypeCode = '" & actypecode.Text & "' "
            '    Else
            '        countsql += " And ActivityType_ActivityTypeCode is null "
            '    End If


            '    If acticode.Text <> "" Then
            '        countsql += " And Activity_ActivityCode = '" & acticode.Text & "' "
            '    Else
            '        countsql += " And Activity_ActivityCode is null "
            '    End If



            '    If Val(cmmtdetail.Text) <> 0 Then
            '        countsql += " And CmmtDetail_ID = " & Val(cmmtdetail.Text) & " "
            '    Else
            '        countsql += " And CmmtDetail_ID is null "
            '    End If



            '    If Val(Left(vers.Text, 1)) < 5 Then
            '        If projcode.Text <> "" Then
            '            countsql += " And Project_ProjectCode = '" & projcode.Text & "' "
            '        Else
            '            countsql += " And Project_ProjectCode is null "
            '        End If

            '    End If

            '    If Val(Left(vers.Text, 1)) < 5 Then

            '    Else
            '        If fundedprogram.Text <> "" Then
            '            countsql += " And FundedProgram = '" & fundedprogram.Text & "' "
            '        Else
            '            countsql += " And FundedProgram is null "
            '        End If

            '    End If



            '    If prodcode.Text <> "" Then
            '        countsql += " And Product_ProductCode = '" & prodcode.Text & "' "
            '    Else
            '        countsql += " And Product_ProductCode is null "
            '    End If

            'countsql += " AND (FundCenter_FundCenterCode = '" & fundccode.Text & "')"
            Dim amount As Decimal = 0
            amount = adapterx.GetAmountData(countsql)
            'amount = Val(adapterx.GetCountData(countsql))

            'Response.Write(countsql)
            'Exit Sub

                '     Dim amount As Decimal = GetAmount(fundccode.Text, cmmtcode.Text, vers.Text, pyear.Text)
                'Dim amount = 0
                'Dim radiobox1 As CheckBox = CType(row.FindControl("janselect"), CheckBox)
                'Dim radiobox2 As CheckBox = CType(row.FindControl("febselect"), CheckBox)
                'Dim radiobox3 As CheckBox = CType(row.FindControl("marselect"), CheckBox)
                'Dim radiobox4 As CheckBox = CType(row.FindControl("aprselect"), CheckBox)
                'Dim radiobox5 As CheckBox = CType(row.FindControl("mayselect"), CheckBox)
                'Dim radiobox6 As CheckBox = CType(row.FindControl("junselect"), CheckBox)
                'Dim radiobox7 As CheckBox = CType(row.FindControl("julselect"), CheckBox)
                'Dim radiobox8 As CheckBox = CType(row.FindControl("augselect"), CheckBox)
                'Dim radiobox9 As CheckBox = CType(row.FindControl("sepselect"), CheckBox)
                'Dim radiobox10 As CheckBox = CType(row.FindControl("octselect"), CheckBox)
                'Dim radiobox11 As CheckBox = CType(row.FindControl("novselect"), CheckBox)
                'Dim radiobox12 As CheckBox = CType(row.FindControl("decselect"), CheckBox)

                'If radiobox1.Checked = True Then
                '    addedstring += "1;"
                'End If
                'If radiobox2.Checked = True Then
                '    addedstring += "2;"
                'End If
                'If radiobox3.Checked = True Then
                '    addedstring += "3;"
                'End If
                'If radiobox4.Checked = True Then
                '    addedstring += "4;"
                'End If
                'If radiobox5.Checked = True Then
                '    addedstring += "5;"
                'End If
                'If radiobox6.Checked = True Then
                '    addedstring += "6;"
                'End If
                'If radiobox7.Checked = True Then
                '    addedstring += "7;"
                'End If
                'If radiobox8.Checked = True Then
                '    addedstring += "8;"
                'End If
                'If radiobox9.Checked = True Then
                '    addedstring += "9;"
                'End If
                'If radiobox10.Checked = True Then
                '    addedstring += "10;"
                'End If
                'If radiobox11.Checked = True Then
                '    addedstring += "11;"
                'End If

                'If radiobox12.Checked = True Then
                '    addedstring += "12;"

                'End If

            's_id.Text
            DivideData(info_array, info_array.Length - 1, amount, s_id.Text)
        Next

        ScriptText = "<script>alert('บันทึกเรียบร้อยแล้ว');</script>"
        CloseText = "<script>window.parent.TINY.box.hide();</script>"
    End Sub
    Sub DivideData(ByVal dataarray As Array, ByVal counter As Decimal, ByVal Amount As Decimal, ByVal idupdate As Decimal)
        Dim numbers As Decimal = Amount
        Dim moduloset As Decimal
        Dim numberset As Decimal
        Dim currentmodulo As Decimal
        Dim numberround As Decimal
        Dim dat1, dat2, dat3, dat4, dat5, dat6, dat7, dat8, dat9, dat10, dat11, dat12 As Decimal
   
        If counter > 1 Then
            For j = 0 To counter - 1
                numberset = numbers \ (counter)
                numberround = Math.ceiling(numberset / 10) * 10
                currentmodulo = numbers Mod (counter)

                Dim currentnumber = numbers / (counter)


                If currentmodulo > 4 Then
                    moduloset += (currentnumber - numberset) - Math.Abs(numberround - numberset)
                Else
                    moduloset += (currentnumber - numberset) + Math.Abs(numberround - numberset)
                End If




            Next
            Dim sumset = moduloset
         
            For i = 0 To counter - 1
                If i <> counter - 1 Then
                    Select Case Val(dataarray(i))
                        Case 1 : dat1 = numberround
                        Case 2 : dat2 = numberround
                        Case 3 : dat3 = numberround
                        Case 4 : dat4 = numberround
                        Case 5 : dat5 = numberround
                        Case 6 : dat6 = numberround
                        Case 7 : dat7 = numberround
                        Case 8 : dat8 = numberround
                        Case 9 : dat9 = numberround
                        Case 10 : dat10 = numberround
                        Case 11 : dat11 = numberround
                        Case 12 : dat12 = numberround
                    End Select
                Else
                    If i - 1 >= 0 Then


                        Select Case Val(dataarray(i))
                            Case 1 : dat1 = numbers - (numberround * (counter - 1))
                            Case 2 : dat2 = numbers - (numberround * (counter - 1))
                            Case 3 : dat3 = numbers - (numberround * (counter - 1))
                            Case 4 : dat4 = numbers - (numberround * (counter - 1))
                            Case 5 : dat5 = numbers - (numberround * (counter - 1))
                            Case 6 : dat6 = numbers - (numberround * (counter - 1))
                            Case 7 : dat7 = numbers - (numberround * (counter - 1))
                            Case 8 : dat8 = numbers - (numberround * (counter - 1))
                            Case 9 : dat9 = numbers - (numberround * (counter - 1))
                            Case 10 : dat10 = numbers - (numberround * (counter - 1))
                            Case 11 : dat11 = numbers - (numberround * (counter - 1))
                            Case 12 : dat12 = numbers - (numberround * (counter - 1))
                        End Select
                    End If

                End If
                moduloset = 0
                'MsgBox(dat1 & " " & dat2)

                Dim adapter As New SplitBudgetDSTableAdapters.Budget12TableAdapter
                adapter.UpdateBudget122(dat1, dat2, dat3, dat4, dat5, dat6, dat7, dat8, dat9, dat10, dat11, dat12, Now(), Session("users"), idupdate)
            Next
        ElseIf counter = 1 Then
            Select Case Val(dataarray(0))
                Case 1 : dat1 = Amount
                Case 2 : dat2 = Amount
                Case 3 : dat3 = Amount
                Case 4 : dat4 = Amount
                Case 5 : dat5 = Amount
                Case 6 : dat6 = Amount
                Case 7 : dat7 = Amount
                Case 8 : dat8 = Amount
                Case 9 : dat9 = Amount
                Case 10 : dat10 = Amount
                Case 11 : dat11 = Amount
                Case 12 : dat12 = Amount
            End Select
            Dim adapter As New SplitBudgetDSTableAdapters.Budget12TableAdapter
            adapter.UpdateBudget122(dat1, dat2, dat3, dat4, dat5, dat6, dat7, dat8, dat9, dat10, dat11, dat12, Now(), Session("users"), idupdate)
        End If
    End Sub
End Class
